package com.learn.springcloud.hystrixexplainindetailconsumer.request.command.annotation;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.client.RestTemplate;

/**
 * 通过注解的方式创建 HystrixCommand 的实现
 * @Time : 2018/8/3 10:26
 * @Version : 1.0
 */
public class UserAnnotation {

    private RestTemplate restTemplate;

    public UserAnnotation(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @HystrixCommand(fallbackMethod = "sayHelloFallback")
    public String sayHello(String name) {
        System.out.println("RequestCacheAnnotation Running : " + name);
        String url = "http://hystrix-explain-in-detail-provider/request/command/say/hello?name={1}";
        boolean flag = true;
        if(flag){
            throw new RuntimeException();
        }
        return restTemplate.getForObject(url, String.class, name);
    }

    @HystrixCommand(fallbackMethod = "exceptionHandler")
    public String sayHelloFallback(String name){
        return "error, " + name;
    }

    public String exceptionHandler(String name){
        return "error";
    }
}
